package 剑指offer_力扣版.Month07.day05字符串;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class 字符串中的所有变位词 {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> list = new ArrayList<>();
        if(p.length() > s.length()) return list;
        int[] arrS = new int[26];
        int[] arrP = new int[26];
        for (int i = 0; i < p.length(); i++) {
            arrS[s.charAt(i) - 'a']++;
            arrP[p.charAt(i) - 'a']++;
        }
        if(Arrays.equals(arrS,arrP)){
            list.add(0);
        }
        int left = 0;
        int right = p.length();
        while(right < s.length()){
            arrS[s.charAt(left) - 'a']--;
            arrS[s.charAt(right) - 'a']++;
            left++;
            right++;
            if(Arrays.equals(arrS,arrP)){
                list.add(left);
            }
        }
        return list;
    }
}
